﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using rock.DAL;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using rock.Model;
using rock.UI.common;

namespace rock.BLL
{
   public class BllFEE_SHAOYAJING
    {
       DalFEE_SHAOYAJING aDalFEE_SHAOYAJING = new DalFEE_SHAOYAJING();
       DalFEE_TUIYAJING aDalFEE_TUIYAJING = new DalFEE_TUIYAJING();
       public void deleteYajing(int SEQID)
       {
            Hashtable sqlParamInfo = new Hashtable();

           StringBuilder shao_strSql = new StringBuilder();
           shao_strSql.Append("delete from FEE_SHAOYAJING ");
           shao_strSql.Append(" where SEQID=@SEQID ");
           SqlParameter[] shao_parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)			};
           shao_parameters[0].Value = SEQID;

           sqlParamInfo.Add(shao_strSql, shao_parameters);



           StringBuilder tui_strSql = new StringBuilder();
           tui_strSql.Append("delete from FEE_TUIYAJING ");
           tui_strSql.Append(" where RENT_DEOSIT_FEE_SEQID=@RENT_DEOSIT_FEE_SEQID");
           SqlParameter[] tui_parameters = {
					new SqlParameter("@RENT_DEOSIT_FEE_SEQID", SqlDbType.Int,4)
			};
           tui_parameters[0].Value = SEQID;


           sqlParamInfo.Add(tui_strSql, tui_parameters);

           DbHelperSQL.ExecuteSqlTran(sqlParamInfo);
       }

       /// <summary>
       /// 获取要退的押金数Decimal[] { yajing, yitui }
       /// </summary>
       /// <param name="SEQID"></param>
       /// <returns>Decimal[] { yajing, yitui }</returns>
       public Decimal[] getNeedTuiYajing(int SEQID) {
           String sql = @"
            select 
            ISNULL(( select MONEY  AS A from FEE_SHAOYAJING where SEQID = @SEQID ), 0) AS YAJING,
            ISNULL(( SELECT sum(RECEDE_FEE)  AS B FROM FEE_TUIYAJING WHERE RENT_DEOSIT_FEE_SEQID = @RENT_DEOSIT_FEE_SEQID ), 0) AS  YITUI
            ";
           SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4),
                    new SqlParameter("@RENT_DEOSIT_FEE_SEQID", SqlDbType.Int,4)
			};
           parameters[0].Value = SEQID;
           parameters[1].Value = SEQID;

           DataSet ds = DbHelperSQL.Query(sql, parameters);
           Decimal yajing = (Decimal)ds.Tables[0].Rows[0]["YAJING"];
           Decimal yitui = (Decimal)ds.Tables[0].Rows[0]["YITUI"];

           return new Decimal[] { yajing, yitui };

       }

       public void deleteTuiYajing(int seqidInt ,int RENT_DEOSIT_FEE_SEQID)
       {
           Hashtable sqlParamInfo = new Hashtable();

           StringBuilder strSql = new StringBuilder();
           strSql.Append("delete from FEE_TUIYAJING ");
           strSql.Append(" where SEQID=@SEQID");
           SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
           parameters[0].Value = seqidInt;
           sqlParamInfo.Add(strSql, parameters);


           String updateSql = "UPDATE FEE_SHAOYAJING SET STATUS  = @STATUS WHERE SEQID = @SEQID";
           SqlParameter[] updateParameters = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
          };
           updateParameters[0].Value = Constant.FEE_YAJIN_STATUS_YITEIBUFENG;
           updateParameters[1].Value = RENT_DEOSIT_FEE_SEQID;

           sqlParamInfo.Add(updateSql, updateParameters);

           DbHelperSQL.ExecuteSqlTran(sqlParamInfo);

       }

       public void addTuiYajing(ModelFEE_TUIYAJING model)
       {
           Hashtable sqlParamInfo = new Hashtable();
           StringBuilder strSql = new StringBuilder();
           strSql.Append("insert into FEE_TUIYAJING(");
           strSql.Append("OPERA_MAN,RENT_DEOSIT_FEE_SEQID,OPEAR_TIME,RECEDE_FEE,RECORD_TIME,NOTE)");
           strSql.Append(" values (");
           strSql.Append("@OPERA_MAN,@RENT_DEOSIT_FEE_SEQID,@OPEAR_TIME,@RECEDE_FEE,@RECORD_TIME,@NOTE)");
           strSql.Append(";select @@IDENTITY");
           SqlParameter[] parameters = {
					new SqlParameter("@OPERA_MAN", SqlDbType.VarChar,30),
					new SqlParameter("@RENT_DEOSIT_FEE_SEQID", SqlDbType.Int,4),
					new SqlParameter("@OPEAR_TIME", SqlDbType.DateTime),
					new SqlParameter("@RECEDE_FEE", SqlDbType.Decimal,9),
					new SqlParameter("@RECORD_TIME", SqlDbType.DateTime),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300)};
           parameters[0].Value = model.OPERA_MAN;
           parameters[1].Value = model.RENT_DEOSIT_FEE_SEQID;
           parameters[2].Value = model.OPEAR_TIME;
           parameters[3].Value = model.RECEDE_FEE;
           parameters[4].Value = model.RECORD_TIME;
           parameters[5].Value = model.NOTE;

           sqlParamInfo.Add(strSql, parameters);

          Decimal[] infos =  this.getNeedTuiYajing(model.RENT_DEOSIT_FEE_SEQID);


          int status = Constant.FEE_YAJIN_STATUS_YISHAO;
          if ((infos[1] + model.RECEDE_FEE) >= infos[0])
          {
              status = Constant.FEE_YAJIN_STATUS_YITEI;
          }
          else {
              status = Constant.FEE_YAJIN_STATUS_YITEIBUFENG;
          }

          String updateSql = "UPDATE FEE_SHAOYAJING SET STATUS  = @STATUS WHERE SEQID = @SEQID";
          SqlParameter[] updateParameters = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
          };
          updateParameters[0].Value = status;
          updateParameters[1].Value = model.RENT_DEOSIT_FEE_SEQID;

          sqlParamInfo.Add(updateSql, updateParameters);
          DbHelperSQL.ExecuteSqlTran(sqlParamInfo);
       }
    }
}
